package com.ruoyi.hualian.controller;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.hualian.domain.*;
import com.ruoyi.hualian.service.*;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 华链同步轮
 * 
 * @author hujian
 */
@RestController
@RequestMapping("/hualian/type")
public class HlTypeController extends BaseController
{
    @Autowired
    private IHlTypeService hlTypeService;

    @Autowired
    private IHlTimingPulleyService hlTimingPulleyService;

    @Autowired
    private IHlMaterialService hlMaterialService;

    @Autowired
    private IHlBandwidthService hlBandwidthService;

    @Autowired
    private IHlShapeAxleService hlShapeAxleService;

    @Autowired
    private IHlOtherCommodityDetailsService hlOtherCommodityDetailsService;

    /**
     * 查询同步轮类型集合
     */
    @GetMapping("/tpTypeList")
    public AjaxResult selectTpTypeList() {
        List<String> tpTypeList = hlTimingPulleyService.selectTpTypeList();
        for (int i = 0; i < tpTypeList.size(); i++) {
            String type = tpTypeList.get(i);
            if ("TP".equals(type)) {
                tpTypeList.set(i, type + "(同步轮)");
            } else if ("IG".equals(type)) {
                tpTypeList.set(i, type + "(带齿惰轮)");
            }
        }
        return AjaxResult.success(tpTypeList);
    }

    /**
     * 查询同步轮材质集合
     */
    @GetMapping("/tpMaterialList")
    public AjaxResult selectTpMaterialList() {
        List<HlMaterial> hlMaterialList = hlMaterialService.selectTpMaterialList();
        List<String> strings = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder();
        for (HlMaterial hlMaterial : hlMaterialList) {
            stringBuilder.append(hlMaterial.getMaterialMark()).append("(").append(hlMaterial.getMaterialName()).append(")");
            strings.add(stringBuilder.toString());
            stringBuilder = new StringBuilder();
        }
        return AjaxResult.success(strings);
    }

    /**
     * 查询同步轮齿形
     */
    @GetMapping("/tpToothList/{tpType}")
    public AjaxResult selectTpToothList(@PathVariable String tpType) {
        int index = tpType.indexOf("(");
        if (index != -1) {
            tpType = tpType.substring(0, index);
        }
        List<HlBandwidth> hlBandwidths = new ArrayList<>();
        if (ObjectUtils.isEmpty(tpType) || "undefined".equals(tpType)) {
            hlBandwidths = hlBandwidthService.selectHlBandwidthList(new HlBandwidth());
        } else {
            HlBandwidth hlBandwidth = new HlBandwidth();
            hlBandwidth.setBandwidthAffiliation(tpType);
            hlBandwidths = hlBandwidthService.selectList(hlBandwidth);
        }
        List<String> strings = new ArrayList<>();
        for (HlBandwidth hlBandwidth : hlBandwidths) {
            String tooth = hlBandwidth.getBandwidth().substring(0, hlBandwidth.getBandwidth().length() - 3);
            strings.add(tooth);
        }
        // 使用LinkedHashSet去除重复元素
        Set<String> uniqueStrings = new LinkedHashSet<>(strings);
        // 将去重后的元素转换回List<String>
        List<String> result = new ArrayList<>(uniqueStrings);

        /*int fiveIndex = result.indexOf("H5M");
        if (fiveIndex != -1) {
            // 将5与第一个元素交换
            String temp = result.get(0);
            result.set(0, result.get(fiveIndex));
            result.set(fiveIndex, temp);
        }*/

        return AjaxResult.success(result);
    }

    /**
     * 查询同步轮带宽集合
     */
    @GetMapping("/tpBandwidth/{tpTooth}")
    public AjaxResult selectTpBandwidthList(@PathVariable String tpTooth) {
        List<HlBandwidth> hlBandwidths = hlBandwidthService.selectHlBandwidthList(new HlBandwidth());
        List<String> strings = new ArrayList<>();
        for (HlBandwidth hlBandwidth : hlBandwidths) {
            String tooth = hlBandwidth.getBandwidth().substring(0, hlBandwidth.getBandwidth().length() - 3);
            if (ObjectUtils.equals(tpTooth, tooth)) {
                String bandwidth = hlBandwidth.getBandwidth().substring(hlBandwidth.getBandwidth().length() - 3);
                strings.add(bandwidth);
            }

        }
        // 使用LinkedHashSet去除重复元素
        Set<String> uniqueStrings = new LinkedHashSet<>(strings);
        // 将去重后的元素转换回List<String>
        List<String> result = new ArrayList<>(uniqueStrings);
        return AjaxResult.success(result);
    }

    /**
     * 根据前端参数查询同步轮齿数集合
     */
    @GetMapping("/tpTeethNum/{parameter}")
    public AjaxResult selectTpBandwidthListForParameter(@PathVariable String parameter) {
        List<String> strings = hlTimingPulleyService.selectTpBandwidthListForParameter(parameter);
        return AjaxResult.success(strings);
    }

    /**
     * 查询同步轮形状集合
     */
    @GetMapping("/tpShape/{tpType}")
    public AjaxResult selectTpShapeList(@PathVariable String tpType) {
        List<HlShapeAxle> hlShapeAxles = new ArrayList<>();
        if (ObjectUtils.isEmpty(tpType) || "undefined".equals(tpType)) {
            hlShapeAxles = hlShapeAxleService.selectHlShapeAxleList(new HlShapeAxle());
        } else {
            if (tpType.contains("TP")) {
                tpType = "TP";
            } else if (tpType.contains("IG")) {
                tpType = "IG";
            }
            HlShapeAxle hlShapeAxle = new HlShapeAxle();
            hlShapeAxle.setAffiliation(tpType);
            hlShapeAxles = hlShapeAxleService.selectList(hlShapeAxle);
        }


        List<String> strings = new ArrayList<>();
        StringBuilder stringBuilder = new StringBuilder();
        for (HlShapeAxle hlShapeAxle : hlShapeAxles) {
            stringBuilder.append(hlShapeAxle.getSphapeMark()).append("(").append(hlShapeAxle.getSphapeName()).append(")");
            strings.add(stringBuilder.toString());
            stringBuilder = new StringBuilder();
        }
        // 使用LinkedHashSet去除重复元素
        Set<String> uniqueStrings = new LinkedHashSet<>(strings);
        // 将去重后的元素转换回List<String>
        List<String> result = new ArrayList<>(uniqueStrings);
        return AjaxResult.success(result);
    }

    /**
     * 查询同步轮轴孔类型集合
     */
    @GetMapping("/tpAxleHoleType")
    public AjaxResult selectTpAxleHoleTypeList(@RequestParam("tpType") String tpType,
                                               @RequestParam("chooseTpShape") String chooseTpShape) {
        List<String> result = new ArrayList<>();
        chooseTpShape = chooseTpShape.substring(0, 1);
        if (tpType.contains("TP")) {
            tpType = "TP";
        } else if (tpType.contains("IG")) {
            tpType = "IG";
        }
        if ("TP".equals(tpType)) {
            HlShapeAxle shapeAxle = new HlShapeAxle();
            shapeAxle.setSphapeMark(chooseTpShape);
            shapeAxle.setAffiliation(tpType);
            List<HlShapeAxle> hlShapeAxles = hlShapeAxleService.selectList(shapeAxle);
            List<String> strings = new ArrayList<>();
            StringBuilder stringBuilder = new StringBuilder();
            for (HlShapeAxle hlShapeAxle : hlShapeAxles) {
                stringBuilder.append(hlShapeAxle.getAxleMark()).append("(").append(hlShapeAxle.getAxleName()).append(")");
                strings.add(stringBuilder.toString());
                stringBuilder = new StringBuilder();
            }
            // 使用LinkedHashSet去除重复元素
            Set<String> uniqueStrings = new LinkedHashSet<>(strings);
            // 将去重后的元素转换回List<String>
            result = new ArrayList<>(uniqueStrings);
        } else if ("IG".equals(tpType)) {
            List<HlOtherCommodityDetails> hlOtherCommodityDetailsList = hlOtherCommodityDetailsService.selectTpAxleHoleTypeList(chooseTpShape);
            for (int i = 0; i < hlOtherCommodityDetailsList.size(); i++) {
                result.add(hlOtherCommodityDetailsList.get(i).getBearingType());
            }
        }
        return AjaxResult.success(result);
    }

    /**
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */


    /**
     * 查询同步轮列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HlType hlType)
    {
        startPage();
        List<HlType> list = hlTypeService.selectHlTypeList(hlType);
        return getDataTable(list);
    }

    /**
     * 导出同步轮列表
     */
    @Log(title = "同步轮", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HlType hlType)
    {
        List<HlType> list = hlTypeService.selectHlTypeList(hlType);
        ExcelUtil<HlType> util = new ExcelUtil<HlType>(HlType.class);
        util.exportExcel(response, list, "同步轮数据");
    }

    /**
     * 获取同步轮详细信息
     */
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId)
    {
        return success(hlTypeService.selectHlTypeByTypeId(typeId));
    }

    /**
     * 新增同步轮
     */
    @Log(title = "同步轮", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HlType hlType)
    {
        return toAjax(hlTypeService.insertHlType(hlType));
    }

    /**
     * 修改同步轮
     */
    @Log(title = "同步轮", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HlType hlType)
    {
        return toAjax(hlTypeService.updateHlType(hlType));
    }

    /**
     * 删除同步轮
     */
    @Log(title = "同步轮", businessType = BusinessType.DELETE)
	@DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable Long[] typeIds)
    {
        return toAjax(hlTypeService.deleteHlTypeByTypeIds(typeIds));
    }
}
